package com.tanhua.manager.service;

import com.tanhua.domain.db.AnalysisByDay;
import com.tanhua.domain.vo.AnalysisSummaryVo;
import com.tanhua.dubbo.api.db.AnalysisByDayApi;
import com.tanhua.dubbo.api.db.LogApi;
import com.tanhua.manager.utils.ComputeUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.joda.time.DateTime;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class AnalysisService {

    @Reference
    private LogApi logApi;
    @Reference
    private AnalysisByDayApi analysisByDayApi;

    public void analysis(String todayStr) {

        AnalysisByDay analysisByDay = new AnalysisByDay();

        analysisByDay.setRecordDate(new DateTime(todayStr).toDate());


        Integer numRegistered =  logApi.queryNumRegisteredOrLogin(todayStr,"0102");
        analysisByDay.setNumRegistered(numRegistered); //注册人数


        Integer numLogin =  logApi.queryNumRegisteredOrLogin(todayStr,"0101");
        analysisByDay.setNumLogin(numLogin); //登录人数


        Integer numActive =  logApi.queryNumActive(todayStr);
        analysisByDay.setNumActive(numActive); //活跃人数




        String yesTodayStr = new DateTime(todayStr).minusDays(1).toString("yyyy-MM-dd");
        Integer numRetention1d =  logApi.queryNumRetention1d(todayStr,yesTodayStr);
        analysisByDay.setNumRetention1d(numRetention1d); //次日留存人数

//        如果当前的数据存在就应该修改

        AnalysisByDay analysisByDay1 = analysisByDayApi.queryByToday(todayStr);
        if(analysisByDay1==null){
            analysisByDayApi.save(analysisByDay);
        }else{
            analysisByDay.setId(analysisByDay1.getId());
            analysisByDayApi.updateById(analysisByDay);
        }




    }

    public ResponseEntity summary(String todayStr) {
        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

//        select sum(num_registered) from tb_analysis_by_day

        Long cumulativeUsers = analysisByDayApi.queryCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(cumulativeUsers); // 累计用户数


        //        select sum(num_active) from tb_analysis_by_day where record_date>='2021-07-11'
        String passMonth = new DateTime(todayStr).minusDays(30).toString("yyyy-MM-dd");
        Long activePassMonth = analysisByDayApi.queryActivePassDays(passMonth);
        analysisSummaryVo.setActivePassMonth(activePassMonth); //过去30天活跃用户数

        String passWeek = new DateTime(todayStr).minusDays(7).toString("yyyy-MM-dd");
        Long activePassWeek = analysisByDayApi.queryActivePassDays(passWeek);
        analysisSummaryVo.setActivePassWeek(activePassWeek); //过去7天活跃用户

        String yesTodayStr = new DateTime(todayStr).minusDays(1).toString("yyyy-MM-dd");

//        select num_registered from tb_analysis_by_day where record_date='2021-08-11'
        Long newUsersToday = analysisByDayApi.queryNumByColumnAndDate("num_registered",todayStr);
        Long newUsersYesToday = analysisByDayApi.queryNumByColumnAndDate("num_registered",yesTodayStr);
        analysisSummaryVo.setNewUsersToday(newUsersToday); //今日新增用户数量
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday.intValue(), newUsersYesToday.intValue());
        analysisSummaryVo.setNewUsersTodayRate(newUsersTodayRate); //今日新增用户涨跌率

//        select num_login from tb_analysis_by_day where record_date='2021-08-11'
        Long loginTimesToday = analysisByDayApi.queryNumByColumnAndDate("num_login",todayStr);
        Long loginTimesYesToday = analysisByDayApi.queryNumByColumnAndDate("num_login",yesTodayStr);

        analysisSummaryVo.setLoginTimesToday(loginTimesToday);  //今日登录次数
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday.intValue(), loginTimesYesToday.intValue());
        analysisSummaryVo.setLoginTimesTodayRate(loginTimesTodayRate); //今日登录用户涨跌率



        //        select num_active from tb_analysis_by_day where record_date='2021-08-11'
        Long activeUsersToday = analysisByDayApi.queryNumByColumnAndDate("num_active",todayStr);
        Long activeUsersYesToday = analysisByDayApi.queryNumByColumnAndDate("num_active",yesTodayStr);

        analysisSummaryVo.setActiveUsersToday(activeUsersToday);   //今日活跃用户数量
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday.intValue(), activeUsersYesToday.intValue());
        analysisSummaryVo.setActiveUsersTodayRate(activeUsersTodayRate);  //今日活跃用户涨跌率



        return ResponseEntity.ok(analysisSummaryVo);



    }
}
